/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Datos;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import java.util.LinkedList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author Analia
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    //*******************************************************************//

    public static Vector<String> detalleSQL_ResultSet_Int(ResultSet r, int x) {
        Vector<String> vector = new Vector<String>();

        try {

            while (r.next()) {
                vector.add(r.getString(x));
            }

        } catch (Exception ex) {
            Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return vector;
    }

    public static String identificador(ResultSet r, int x) {
        try {
            r.next();
            return r.getString(x);

        } catch (Exception e) {
            Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, e);
        }
        return "";
    }

    public static int CantTuplas(ResultSet Rs) {
        int cont = 0;
        try {

            while (Rs.next()) {
                cont++;
            }

        } catch (Exception e) {
            Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, e);
        }

        return cont;
    }

    public static String datos(ResultSet Rs) {
        String s = "";

        try {
            Rs.next();

            for (int i = 1; i <= Rs.getMetaData().getColumnCount(); i++) {
                s = s + Rs.getString(i) + ",";
            }

            s = s.substring(0, s.length() - 1);

        } catch (Exception ex) {
            System.out.println("Ocurrio un Error : HibernateUtil.datos");
            Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return s;
    }

    public static boolean existe(ResultSet Rs) {
        try {
            return Rs.next();
        } catch (Exception ex) {

            System.out.println("error");
            Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, ex);

        }
        return false;
    }

    public static String mostrarTablaCols(ResultSet r, String nombresCabecera[], String nombreColumnas[]) {
        String stabla = "";
        if (r != null) {
            try {
                String line = "-";
                int ncols = nombreColumnas.length;
                String scabecera = "";
                for (int i = 0; i < ncols; i++) {
                    scabecera += nombresCabecera[i] + "\t";
                }
                stabla = "";
                int mlr = scabecera.length();
                while (r.next()) {
                    String rlinea = "";
                    for (int i = 0; i < ncols; i++) {
                        Object object = r.getObject(nombreColumnas[i]);
                        if (object != null) {
                            rlinea += object.toString().trim() + "\t";
                        } else {
                            rlinea += "    " + "\t";
                        }
                    }
                    if (rlinea.trim().length() > mlr) {
                        mlr = rlinea.length();
                    }
                    stabla += rlinea + "\n";
                }

                for (int i = 0; i < mlr; i++) {
                    line += "-";
                }

                stabla = line + "\n" + scabecera + "\n" + line + "\n" + stabla + line;

            } catch (SQLException ex) {
                Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            return stabla;
        } else {
            return "INFORMACION : NO SE ENCONTRARON RESULTADOS";
        }

    }

    public static String mostrarTuplas(ResultSet r, String titulo, String nombresCabecera[],
            String nombreColumnas[]) {

        String stabla = "";
        if (r != null) {
            String strVacio = "                                                                       "
                    + "                                                                               "
                    + "                                                                               "
                    + "                                                                               "
                    + "                                                                               ";
            String strLinea = "-----------------------------------------------------------------------"
                    + "---------------------------------------------------------------------------------"
                    + "---------------------------------------------------------------------------------"
                    + "---------------------------------------------------------------------------------";

            try {
                int ncols = nombreColumnas.length;
                LinkedList<Integer> listaStrMayor = new LinkedList<Integer>();
                String[][] tabla = new String[200][ncols];
                // inicializo mi lista de length de cada columna
                for (int i = 0; i < ncols; i++) {
                    listaStrMayor.add(i, nombresCabecera[i].length());
                }
                // cargar las filas
                int nfils = 0;
                while (r.next()) {
                    for (int columna = 0; columna < ncols; columna++) {
                        Object object = r.getObject(nombreColumnas[columna]);
                        if (object != null) {
                            tabla[nfils][columna] = object.toString().trim();
                            if (tabla[nfils][columna].length() > listaStrMayor.get(columna)) {
                                listaStrMayor.set(columna, tabla[nfils][columna].length());
                            }
                        } else {
                            tabla[nfils][columna] = "";
                        }
                    }
                    nfils++;
                }

//                if (nfils > 0) {
                // LINEA DE DIVICION
                int longitudCad = 0;
                String line = "";
                for (int i = 0; i < listaStrMayor.size(); i++) {
                    longitudCad = longitudCad + listaStrMayor.get(i) + 3;
                    line += strLinea.substring(0, listaStrMayor.get(i) + 3);
                }
                // TITULO DE LA CONSULTA
                if (!titulo.equals("")) {
                    int x = (longitudCad / 2) - (titulo.length() / 2);
                    if (x > 0) {
                        String Titulo = strVacio.substring(0, x);
                        stabla += Titulo + titulo.toUpperCase() + "\n";
                    }

                }
                // CABECERA DE LA TABLA
                stabla += line + "\n";
                for (int columna = 0; columna < ncols; columna++) {
                    String aContatenar = strVacio.substring(0, listaStrMayor.get(columna) - nombresCabecera[columna].length());
                    stabla += nombresCabecera[columna] + aContatenar + "   ";
                }
                stabla += "\n" + line + "\n";
                // FILAS DE LA TABLA
                for (int filas = 0; filas < nfils; filas++) {
                    for (int columna = 0; columna < ncols; columna++) {
                        String aContatenar = strVacio.substring(0, listaStrMayor.get(columna) - tabla[filas][columna].length());
                        stabla += tabla[filas][columna] + aContatenar + "   ";
                    }
                    stabla += "\n";
                }
                stabla += line + "\n";
//                } else {
//                    stabla = "INFORMACION : NO EXISTEN DATOS ";
//                }
            } catch (SQLException ex) {
                Logger.getLogger(HibernateUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            stabla += "ERROR : TABLA VACIA";
        }

        return stabla;
    }
    //********************************************************************//
}
